package com.itheima.health.service.impl;
import cn.hutool.core.util.RandomUtil;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.SetmealQueryDao;
import com.itheima.health.dto.SmsLoginParam;
import com.itheima.health.exception.BusinessRuntimeException;

import com.itheima.health.service.MobileLoginService;
import com.itheima.health.util.AliyunSmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;




@Service
@Slf4j
public class MobileLoginServiceImpl implements MobileLoginService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetmealQueryDao setmealQueryDao;
    @Autowired
    AliyunSmsUtils aliyunSmsUtils;

    /**
     * 发送验证码
     * @param type
     * @param telephone
     */
    public void send(String type, String telephone) {
        String code = RandomUtil.randomNumbers(4);//数字
        SendSmsResponse response ;
        try {
            response = aliyunSmsUtils.sendSms(telephone, code);
        } catch (ClientException e) {
            throw new BusinessRuntimeException(MessageConst.SEND_VALIDATECODE_FAIL);

        }
        log.info("短信接口返回的数据---");
        log.info("Code=" + response.getCode());
        log.info("Message=" + response.getMessage());
        log.info("RequestId=" + response.getRequestId());
        redisTemplate.opsForValue().set(telephone, code);
    }

    /**
     * 手机登录
     * @param smsLoginParam
     * @return
     */
    public String smsLogin(SmsLoginParam smsLoginParam) {
        //验证短信
        String code= (String) redisTemplate.opsForValue().get(smsLoginParam.getTelephone());
        return code;
    }


}